/// <reference path="../_all.ts" />

module Application.Controllers {

    export class UserController implements Application.Interfaces.Controllers.IController {

        public static $inject = [
            '$rootScope',
            '$scope',
            '$location',
            'UserService',
            'TranslationService',
        ];

        updateResult: {
            (data): void;
            (data, err): void;
        };

        constructor(public RootScope: Application.Interfaces.Scopes.IRootScope
            , public Scope: Application.Interfaces.Scopes.IUserScope
            , public Location: ng.ILocationService
            , public Users: Application.Interfaces.Services.IUserService
            , public Translations: Application.Interfaces.Services.ITranslationService) {
            this.Scope.vm = this;
            this.Scope.user = this.RootScope.user;
            this.Scope.location = this.Location;
            this.Scope.messages = new Application.Utility.Messages();
            this.Scope.navigations = new Application.Utility.Navigations();

            this.Scope.currentuser = angular.copy(this.Scope.user);
            
                this.Scope.navigations.AddLink(Translations.Translate("SERVERLIST"), "/servers", false, false, Application.Constants.Icons.Servers);
                this.Scope.navigations.AddLink(Translations.Translate("ACTIONS"), "/actions", false, false, Application.Constants.Icons.Actions);
            if (this.Scope.user && this.Scope.user.role && this.Scope.user.role >= 2) {
                this.Scope.navigations.AddLink(Translations.Translate("PLAYERS"), "/players", false, false, Application.Constants.Icons.Bans);
            };
            if (this.Scope.user && this.Scope.user.role && this.Scope.user.role >= 8) {
                this.Scope.navigations.AddLink(Translations.Translate("USERS"), "/users", false, false, Application.Constants.Icons.Users);
            };
                this.Scope.navigations.AddLinkRight(Translations.Translate("ACCOUNT"), "/users/" + this.Scope.user.user_id, true);
                this.Scope.navigations.AddLinkRight(Translations.Translate("LOGOUT"), "/", false);

            this.updateResult = (data, err?) => {
                this.updateResultProxy.apply(this, [data, err]);
            }
        }

        public log(msg: string) {
            //console.log(msg);
        }

        public update() {
            this.log('update');
            var isValid = true;
            var user = <any>this.Scope.currentuser;
            if ((user.newpassword && user.newpassword.length > 0) || (user.newpassword2 && user.newpassword2.length > 0)) {
                if (!user.password || user.password.length <= 0) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, this.Translations.Translate('INVALID'), this.Translations.Translate('FILLYOURPASSWORD'));
                    isValid = false;
                };
                if ((!user.newpassword || user.newpassword.length <= 0) || (!user.newpassword2 || user.newpassword2.length <= 0)) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, this.Translations.Translate('INVALID'), this.Translations.Translate('RETYPEPASSWORD'));
                    isValid = false;
                };
                if (isValid && user.newpassword != user.newpassword2) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, this.Translations.Translate('INVALID'), this.Translations.Translate('RETYPEPASSWORD'));
                    isValid = false;
                };
                if (isValid && user.newpassword.length <= 5) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, this.Translations.Translate('INVALID'), this.Translations.Translate('PASSWORDTOOSHORT'));
                    isValid = false;
                };
            };
            if (isValid) {
                if ((!user.newpassword || user.newpassword.length <= 0) && (!user.email || user.email.length <= 0)) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, this.Translations.Translate('INVALID'), this.Translations.Translate('INVALIDNOCHANGES'));
                    isValid = false;
                };
            };

            if (isValid) {
                this.Users.UpdateUser(this.Scope.currentuser, this.updateResult);
                this.Scope.currentuser = angular.copy(this.Scope.user);
            };
        }

        private updateResultProxy(data, err) {
            this.log('login result');
            if (data != null) {
                var user = <Application.Resources.IUser> data;
                if (user != null && user.login == this.Scope.user.login) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.info, "Info", new Application.Utility.String(this.Translations.Translate('USERUPDATED')).format(user.login));
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", this.Translations.Translate('USERNOTFOUND'));
                };
            } else {
                if (err != null) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, err.data.status + ' ' + err.data.type, err.data.message);
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, '000 Unknown', this.Translations.Translate('UNKNOWNERROR'));
                };
            };
        }
    };
}